package com.zhuge_new.Activity;

import android.arch.lifecycle.ViewModelProvider;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.maning.mndialoglibrary.MToast;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.zhuge_new.MainActivity;
import com.zhuge_new.bean.WxBean;
import com.zhuge_new.http.StringDialogCallback;
import com.zhuge_new.share.BaseHttp;
import com.zhuge_new.untils.ActivityStack;
import com.zhuge_new.untils.DataParse;
import com.zhuge_new.untils.PreferencesUtils;
import com.zhuge_new.untils.StatusBarUtil;
import com.zhuge_new.wxapi.Constacts;
import com.zhuge_new.Base.BaseActivity;
import com.zhuge_new.R;
import com.zhuge_new.bean.EventInfo;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by lenovo on 2018-08-24.
 */

public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private String code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        setContentView(R.layout.activity_login);
        initView();
    }

    private void initView() {
        StatusBarUtil.setStatusBarColor(LoginActivity.this,R.color.logyellow);
        Button WX_login=findViewById(R.id.wx_login);
        WX_login.setOnClickListener(this);
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.wx_login:
                wxLogin();
        }
    }

    //微信登陆第一步，发送请求
    private void wxLogin() {
        IWXAPI WXapi= WXAPIFactory.createWXAPI(this, Constacts.WXAPPID,true);
        WXapi.registerApp(Constacts.WXAPPID);
        SendAuth.Req req=new SendAuth.Req();
        req.scope="snsapi_userinfo";
        req.state="wechat_sdk_demo";
        WXapi.sendReq(req);
    }

    //第二步，获取code
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getWxcode(EventInfo eventInfo){
        code = eventInfo.getInfo();
        OkGo.<String>post(BaseHttp.wx_login)
                .tag(LoginActivity.this)
                .params("code", code)
                .params("type", "APPWX")
                .execute(new StringDialogCallback(baseContext) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                        WxBean wxBean= (WxBean) new DataParse().getBean(response.body(),WxBean.class,0);
                        if (wxBean.getMsgcode()==100){
                            Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                            PreferencesUtils.putString(baseContext,"unionid",wxBean.getObject().getWx_openid());
                            PreferencesUtils.putString(baseContext,"token",wxBean.getObject().getToken());
                            PreferencesUtils.putString(baseContext,"openid",wxBean.getObject().getUserId());
                            PreferencesUtils.putString(baseContext,"wx_nickname",wxBean.getObject().getWx_nickname());
                            PreferencesUtils.putString(baseContext,"wx_headimgurl",wxBean.getObject().getWx_headimgurl());
                            PreferencesUtils.putString(baseContext,"isPass",wxBean.getObject().getIsPass());
                            startActivity(intent);
                            finish();
                        }else {
                            MToast.makeTextShort(baseContext,msg).show();
                        }
                    }
                });

    }

}
